package burubaka.controller.mypage;

import java.util.List;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;

import burubaka.BurubakaConstants;
import burubaka.common.BurubakaUtil;
import burubaka.model.BetTemplate;
import burubaka.model.BetTemplateBody;
import burubaka.model.RaceCardHorse;
import burubaka.model.RaceInfo;
import burubaka.model.Tipster;
import burubaka.model.TotalResult;
import burubaka.service.BetTemplateService;
import burubaka.service.RaceCardHorseService;
import burubaka.service.RaceInfoService;
import burubaka.service.TipsterService;
import burubaka.service.TotalResultService;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class IndexController extends Controller {

	private TipsterService tipsterService = new TipsterService();
	private TotalResultService totalResultService = new TotalResultService();
	private BetTemplateService betTemplateService = new BetTemplateService();
	private RaceInfoService raceInfoService = new RaceInfoService();
	private RaceCardHorseService raceCardHorseService = new RaceCardHorseService();

	@Override
	public Navigation run() throws Exception {

		// ユーザー取得
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		String userId = user.getUserId();
		// 予想屋情報取得
		Tipster tipster = tipsterService.findByUser(userId);
		// 取得できなかった場合は作成
		if (tipster == null) {
			tipster = new Tipster();
			tipster.setUserId(userId);
			// tipster.setNickname(user.getNickname());
		}
		tipster.setLastLoginTime(BurubakaUtil.getCalendarInstance().getTime());
		tipster = tipsterService.regist(tipster);

		if (betTemplateService.findBetTemplateKeyByAncKey(tipster.getKey()) == null) {
			// デフォルトの賭け金テンプレートを基に賭け金テンプレートを登録
			BetTemplate betTemplate = betTemplateService.getDefaultBetTemplate();
			betTemplate.setDefaultFlag(BurubakaConstants.FLAG_OFF);
			List<BetTemplateBody> list = betTemplateService.findBetTemplateBodyByAncKey(betTemplate.getKey());
			betTemplateService.regist(betTemplate, list, tipster.getKey());
		}

		TotalResult totalResult = totalResultService.getThisYearResult(tipster.getKey());
		if (totalResult == null) {
			totalResult = new TotalResult();
			totalResult.setWithdrawalOrder("-");
			totalResult.setWithdrawalRateOrder("-");
			totalResult.setHitOrder("-");
			totalResult.setHitRateOrder("-");
			totalResult.setLastWithdrawalOrder("-");
			totalResult.setLastWithdrawalRateOrder("-");
			totalResult.setLastHitOrder("-");
			totalResult.setLastHitRateOrder("-");
			totalResult.setYear(BurubakaConstants.CURRENT_YEAR);
			totalResultService.regist(totalResult, tipster.getKey());
		}

		RaceInfo raceInfo = raceInfoService.getCurrentRaceInfo();
		if (raceInfo != null) {
			List<RaceCardHorse> raceCardHorseList = raceCardHorseService.findByRaceCardHorseKey(raceInfo.getKey());
			requestScope("raceCardHorseList", raceCardHorseList);
		}
		requestScope("raceInfo", raceInfo);
		requestScope("logoutUrl", userService.createLogoutURL("/top"));
		requestScope("openTab", tipster.getOpenTab());

		return forward("index.jsp");
	}
}
